<?php

class safeAction extends backendAction
{
    public function _initialize() {
        parent::_initialize();
    }
    //safe setting
    public function jsafe(){
    	$type = $this->_get('type','trim');
        if($type){
		        switch ($type) {
		            case 'index':
		                    //配置
							$root_dir = WEB_ROOT.'data/'; // 根目录
							$default_content = ' ';  //生成的index.html文件默认内容
							$dirs = rscandir($root_dir, 'dir');
							$current_path = str_replace('\\', '/', dirname(__FILE__));
							$source_path = $current_path.'/index.html';
							file_put_contents($source_path, $default_content);
							foreach ($dirs as $d)
							{
								copy($source_path, $d.'index.html');
							}
		                break;
		              case 'bom':
		                   //去除bom
							$basedir =WEB_ROOT;
							checkdir($basedir);
		              break;
		              
		              case 'admin':
		                   //更改后台文件入口
		                   $name=$this->_get('name','trim');
		                   $oldname= M('setting')->where(array('name'=>'adm_enter'))->getField('data');
		                   M('setting')->where(array('name'=>'adm_enter'))->save(array('data'=>$name));
		                   rename(WEB_ROOT.$oldname.'.php',WEB_ROOT.$name.'.php');
		                   //清除缓存
		                   $this->qclear();
		              break;
		              case 'putindex':
		                   RA('home/index/index','index.html');
		              break;
		        }
		        $this->ajaxReturn(1);
        }else{
        	  $this->display();
        }

    }
	

}

//重写入
function rewrite ($filename, $data) {
		$filenum = fopen($filename, "w");
		flock($filenum, LOCK_EX);
		fwrite($filenum, $data);
		fclose($filenum);
}
//检查Bom
function checkBOM ($filename) {
			$contents = file_get_contents($filename);
			$charset[1] = substr($contents, 0, 1);
			$charset[2] = substr($contents, 1, 1);
			$charset[3] = substr($contents, 2, 1);
			if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
				 $rest = substr($contents, 3);
				 rewrite ($filename, $rest);
				 return ("<font color=red>BOM found, automatically removed.");
			}
		  return ("BOM Not Found.");
}

//检查目录
function checkdir($basedir){
		if ($dh = opendir($basedir)) {
				 while (($file = readdir($dh)) !== false) {
					 if ($file != '.' && $file != '..'){
						 if (!is_dir($basedir."/".$file)) {
							 checkBOM($basedir/$file);
						 }else{
						     $dirname = $basedir."/".$file;
						     checkdir($dirname);
						  }
					  }
				 }
		   closedir($dh);
		}
}

//生成html
function rscandir($base = '',$return = 'all', &$data = array()  ) {
		$ds = DIRECTORY_SEPARATOR;
		$base = rtrim($base,$ds).$ds;
		$array = array_diff(scandir($base), array('.', '..', '.svn'));
		foreach ($array as $value) {
				if (is_dir($base . $value)) {
						if($return != 'file')$data[] = $base . $value . $ds;
						 $data = rscandir($base . $value . $ds, $return, $data );
				} elseif (is_file($base . $value)) {
						if($return == 'dir') continue;
						  $data[] = $base . $value;
						}
		}
		return $data;
}